home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS20.ADF / Decoder / Compactor (.txt) < prev    next >
AmigaBASIC Source Code  |  1989-01-27  |  5KB  |  123 lines

  1. REM Compactor
  2. REM Steve Michel   9/15/86    815-626-4157
  3. REM 2510 16th Ave.  Sterling  IL  61081
  4.  
  5. REM  get file name to compact
  6.  
  7.   CLS: PRINT 
  8.   INPUT "Enter filename to compact";filename.in$
  9.   PRINT: INPUT "Enter filename for compacted file"; filename.out$
  10.   OPEN "I", #1, filename.in$, 1024
  11.   OPEN "O", #2, filename.out$, 1024
  12.   CLS: PRINT: PRINT "Now reading line  => "
  13.   PRINT:  PRINT "Now writing line  => "
  14.   DIM byte$(300)   ' assumes a single line not longer than 300 bytes
  15.   lines.in = 0: lines.out = 0: bytes.in = 0: bytes.out = 0
  16.  
  17. REM directly copy file attribute bytes
  18.  
  19.   a$ = INPUT$ (1,#1): PRINT #2,a$;
  20.   a$ = INPUT$ (1,#1): PRINT #2,a$;
  21.  
  22. REM start main read / write loop
  23.  
  24. loop:
  25.   byte$(1)  = INPUT$ (1,#1)                 ' get linelength
  26.   linelength = ASC(byte$(1))                ' check for end of BASIC text
  27.   IF linelength = 0 THEN end.of.basic       ' if linelength is zero
  28.                                             ' we're at end of basic text
  29. REM read in line from input file                                       
  30.                                             ' not end of BASIC text, so
  31.   bytes.in = bytes.in + linelength          ' count bytes in and 
  32.   lines.in = lines.in + 1                   ' increment line counter  
  33.   LOCATE 2,22: PRINT lines.in               ' and print it to screen 
  34.   FOR J = 2 TO linelength                   ' read rest of line one byte
  35.     byte$(J) = INPUT$ (1,#1)                ' at a time into the 
  36.   NEXT J                                    ' array  - byte$()
  37.  
  38. REM check for blank line 
  39.  
  40.   byte3   = ASC(byte$(3))                   ' check bytes 3 & 4 of line for 
  41.   byte4   = ASC(byte$(4))                   ' two zeros that indicate a 
  42.   IF byte3 = 0 AND byte4 = 0 THEN loop      ' blank line. if yes, skip line
  43.  
  44. REM check for leading apostrophe
  45.  
  46.   IF byte3 = 58 AND byte4 = 7*25 THEN loop  ' leading apostrophe. so skip
  47.  
  48. REM scan current line for imbedded REMs and '
  49.  
  50.   newlength = 0                             ' no leading REMs or apostrophes
  51.   FOR J = 3 TO linelength                   ' so search for imbedded REMs  
  52.     IF byte$(J) = CHR$(174+1) THEN          ' and apostrophes. if found,
  53.        newlength = J                        ' set position found and force
  54.        J = 1e+09                            ' the loop to end
  55.     END IF
  56.   NEXT J
  57.      
  58.   IF newlength = 0 THEN setup.line          ' no REMs found so save line 
  59.  
  60. REM embedded REM found, check for colon in front of it
  61.  
  62.   FOR J = newlength TO 3 STEP -1            ' start searching line backwards
  63.     IF byte$(J) = CHR$(58) THEN             ' for a colon.  if found, set 
  64.        linelength = J + 1                   ' linelength to that position
  65.        GOTO setup.line                      ' plus 1 and end search and 
  66.     END IF                                  ' setup line to write to new
  67.   NEXT J                                    ' file
  68.  
  69. GOTO loop                                   ' no colon, so skip whole line
  70.                                         
  71. REM this routine sets up the line length, indentation and
  72. REM two zero bytes at the end of the compacted line
  73.  
  74. setup.line:
  75.   byte$(1) = CHR$(linelength)               ' reset line length
  76.   byte$(2) = CHR$(0)                        ' reset line indentation 
  77.   byte$(linelength)   = CHR$(0)             ' set two zero bytes at the 
  78.   byte$(linelength-1) = CHR$(0)             ' end of the line
  79.   
  80.   bytes.out = bytes.out + linelength        ' count # of bytes written
  81.   lines.out = lines.out + 1                 ' keep track of lines written
  82.   LOCATE 4,22: PRINT lines.out              ' and display on screen
  83.   FOR J = 1 TO linelength                   ' now write whole line out to 
  84.     PRINT #2, byte$(J);                     ' to compacted file
  85.   NEXT J
  86.   GOTO loop                                 ' and continue 
  87.  
  88. REM add 2 zero bytes for end of BASIC and check for   
  89. REM ODD / EVEN file lengths and adjust if needed
  90.  
  91. end.of.basic:
  92.   byte$ = INPUT$(1,#1)                      ' get 2nd zero byte from file
  93.   IF bytes.in/2 = INT(bytes.in/2) THEN      ' if even number of bytes read,
  94.     throwaway$ = INPUT$ (1,#1)              ' get rid of extra byte in front
  95.   END IF                                    ' of variable table
  96.   PRINT #2,CHR$(0);                         ' write the end of BASIC markers
  97.   PRINT #2,CHR$(0);  
  98.  
  99.   IF bytes.out/2 = INT(bytes.out/2) THEN    ' if even number of bytes written,
  100.     PRINT #2,CHR$(ASC("J"));                ' add extra byte in front of
  101.   END IF                                    ' variable table. (Thanks, Jay.)
  102.  
  103. REM copy variable table and icon files over
  104.  
  105. finish.up:
  106.   GOSUB copy.rest                           ' copy rest of file
  107.   OPEN "I", #1, filename.in$ + ".info"      ' copy icon information         
  108.   OPEN "O", #2, filename.out$ + ".info"     ' to provide a clickable icon 
  109.   GOSUB copy.rest                           ' copy icon file
  110.   KILL filename.out$ + ".info.info"         ' delete extraneous file   
  111.   LOCATE 6,1: PRINT "All done !"            ' generated during copy process
  112.   END                                       ' and voila !!!
  113.   
  114. copy.rest:  
  115.   byte$ = INPUT$ (1,#1)                     ' get next byte of old file
  116.   PRINT #2, byte$;                          ' send to new file
  117.   IF EOF(1) THEN                            ' check end of old file
  118.     CLOSE #1                                ' done, so tidy everything up
  119.     CLOSE #2
  120.     RETURN                                  ' and go back
  121.   END IF
  122.   GOTO copy.rest                            ' otherwise, continue copying
  123.